const loop = (root) => {
    if(!root) return [];
    let result = [];
    let level = 1;
    const qneue = [root];
    while(qneue.length) {
        let size = qneue.length;
        let listNodes = [];
        for(let i = 0; i < size; i++) {
            let node = qneue.shift();
            listNodes.push(node.val);
            if(node.left) listNodes.push(node.left);
            if(node.right) listNodes.push(node.right);
        }
            if(level % 2 === 0){
                result.push(listNodes.reverse())
            }else {
                result.push(listNodes)
            }
            level++;
    }
    return result;
}

console.log(loop([3,9,20,null,null,15,7]))

